package com.example.readsms;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvDes, tvSms;
    private Button btnSms;
    private String text = "";
    private List<SmsInfo> smsInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    // 初始化控件与查看信息的点击事件
    private void init() {
        tvSms = (TextView) findViewById(R.id.tv_sms);
        tvDes = (TextView) findViewById(R.id.tv_des);
        btnSms = (Button) findViewById(R.id.btn_sms);
        smsInfos = new ArrayList<>();
        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 通过 requestPermissions() 方法动态申请读取短信的权限, 此时会弹出会话框提示是否允许访问系统短信
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, 1);
            }
        });
    }

    // 获取系统短信的信息
    public void getSms() {
        Uri uri = Uri.parse("content://sms/"); // 获取信息的 url
        // 获取 ContentResolver 对象
        ContentResolver resolver = getContentResolver();
        // 通过上述对象查询系统短信
        Cursor cursor = resolver.query(uri, new String[]{"_id", "address", "body"}, null, null, null, null);
        // 依次将查询到的结果存储到 List 中
        if (cursor != null && cursor.getCount() > 0) {
            tvDes.setVisibility(View.VISIBLE);
            tvSms.setVisibility(View.VISIBLE);
            if (smsInfos != null) smsInfos.clear(); // 清除集合中的数据
            text = ""; // 清空 text 的原有数据
            while (cursor.moveToNext()) {
                SmsInfo smsInfo = new SmsInfo(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
                smsInfos.add(smsInfo);
            }
            cursor.close();
        }
        // 将结果打印到界面上
        for (int i = 0; i < smsInfos.size(); i++) {
            text += "手机号码: " + smsInfos.get(i).getAddress() + "\n";
            text += "短信内容" + smsInfos.get(i).getAddress() + "\n\n";
        }
        tvSms.setText(text);
    }

    // 选择是否允许访问短信权限后回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    getSms();
                } else {
                    Toast.makeText(this, "权限" + permissions[i] + "申请失败, 不能读取系统短信", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}